package com.zyq.controller;

import cn.hutool.core.util.StrUtil;
import com.zyq.constant.Constant;
import com.zyq.entity.SysPermission;
import com.zyq.entity.SysUser;
import com.zyq.enuns.ResponseCode;
import com.zyq.service.IPermissionService;
import com.zyq.service.IUserService;
import com.zyq.util.JwtUtil;
import com.zyq.util.R;
import com.zyq.util.RedisUtil;
import com.zyq.util.password.BCrypt;
import com.zyq.vo.req.LoginReqVo;
import com.zyq.vo.resp.LoginRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * author xiaochi
 * date 2024/5/22 11:30
 */
@RestController
public class LoginController extends BaseController{

    @Autowired
    private IUserService userService;
    @Autowired
    private IPermissionService permissionService;

    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo reqVo){
        SysUser sysUser = userService.getUserByUserName(reqVo.getUsername());
        if (ObjectUtils.isEmpty(sysUser)){
            return R.error(ResponseCode.SYSTEM_USERNAME_NOT_EXISTS.getMessage());
        }
        if (!BCrypt.checkpw(reqVo.getPassword(),sysUser.getPassword())){
            return R.error(ResponseCode.SYSTEM_PASSWORD_ERROR.getMessage());
        }
        List<SysPermission> permissionList = permissionService.permissionListByUserId(sysUser.getId());
        List<String> authBtnPerms = permissionList.stream().filter(item -> !StrUtil.isBlank(item.getCode())).map(item -> item.getCode()).collect(Collectors.toList());
        SysUser user = new SysUser();
        user.setId(sysUser.getId());
        user.setUsername(sysUser.getUsername());
        String token = JwtUtil.create(user);
        RedisUtil.set(Constant.User.USER_TOKEN_KEY + user.getUsername(),token,Constant.User.USER_KEY_EXPIRE);
        LoginRespVo loginRespVo = new LoginRespVo();
        BeanUtils.copyProperties(sysUser,loginRespVo);
        loginRespVo.setAuthorization(token);
        loginRespVo.setNickname(sysUser.getNickName());
        loginRespVo.setMenus(permissionService.permissionTreeListMenuFilter(permissionList));
        loginRespVo.setPermissions(authBtnPerms);
        return R.ok(loginRespVo);
    }

    @PostMapping("/logout")
    public R<String> logout(){
        try {
            String username = getUsername();
            String tokenKey = Constant.User.USER_TOKEN_KEY + username;
            String userKey = Constant.User.USER_KEY + username;
            String permissionKey = Constant.User.PERMISSION_KEY + username;
            RedisUtil.del(tokenKey);
            RedisUtil.del(userKey);
            RedisUtil.del(permissionKey);
        }catch (Exception e){
            logger.error("退出登录异常:{}",e);
        }
        return R.ok();
    }
}
